package com.ytjj.qmyx.admin.service.imService.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.constants.NotifyType;
import com.ytjj.qmyx.admin.mapper.ImLeaveMsgMapper;
import com.ytjj.qmyx.admin.model.ImLeaveMsg;
import com.ytjj.qmyx.admin.model.ImLeaveMsgExample;
import com.ytjj.qmyx.admin.model.request.im.ImLeaveMsgRequest;
import com.ytjj.qmyx.admin.model.request.im.MsgEntity;
import com.ytjj.qmyx.admin.model.vo.ImLeaveMsgVO;
import com.ytjj.qmyx.admin.service.imService.ImLeaveMsgService;
import com.ytjj.qmyx.admin.utils.UserUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ImLeaveMsgServiceImpl implements ImLeaveMsgService {

    @Autowired
    private ImLeaveMsgMapper imLeaveMsgMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public CommonPage findList(ImLeaveMsgVO imLeaveMsgVO) {
        Integer userId = UserUtil.getAdminId();
        Integer roleId = imLeaveMsgMapper.findRoleByUserId(userId);
        imLeaveMsgVO.setRoleType(roleId);
        imLeaveMsgVO.setServiceEmail(UserUtil.getPrincipal());
        PageHelper.startPage(imLeaveMsgVO.getPageNum(), imLeaveMsgVO.getPageSize());
        List<ImLeaveMsg> list = imLeaveMsgMapper.findList(imLeaveMsgVO);
        return CommonPage.restPage(list);
    }

    @Override
    public int updateFinshType(ImLeaveMsg record) {
        record.setFinshTime(new Date());
        record.setFinshUser(UserUtil.getPrincipal());
        record.setStatus(1);
        return imLeaveMsgMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public long count() {
        ImLeaveMsgExample example = new ImLeaveMsgExample();
        example.createCriteria().andStatusEqualTo((byte)0);
        return imLeaveMsgMapper.countByExample(example);
    }

    @Override
    public void batchSetFinshUser(ImLeaveMsgRequest imLeaveMsgRequest) {
        ImLeaveMsg record = new ImLeaveMsg();
        record.setFinshUser(imLeaveMsgRequest.getServiceUser());
        record.setAssignee("人工分配");

        ImLeaveMsgExample example = new ImLeaveMsgExample();
        example.createCriteria().andIdIn(imLeaveMsgRequest.getIds());
        int num = imLeaveMsgMapper.updateByExampleSelective(record,example);
        Assert.isTrue(num>0,"分配失败");

        //发消息通知客服
        MsgEntity msgEntity = new MsgEntity();
        msgEntity.setMessage(imLeaveMsgRequest.getServiceUser());
        msgEntity.setType(NotifyType.NOTIFY_TYPE_LEAVE);
        stringRedisTemplate.convertAndSend("channel:imCluster", JSON.toJSONString(msgEntity));
    }
}
